import request from '@/utils/request'

// 获取帖子列表
export function getPostList(params) {
  return request({
    url: '/forum/posts',
    method: 'get',
    params
  })
}

// 获取帖子详情
export function getPostDetail(id) {
  return request({
    url: `/forum/post/${id}`,
    method: 'get'
  })
}

// 创建帖子
export function createPost(data) {
  return request({
    url: '/forum/post',
    method: 'post',
    data
  })
}

// 发布帖子 - 使用POST/forum/post/create接口
export function publishPost(data) {
  return request({
    url: '/forum/post/create',
    method: 'post',
    data
  })
}

// 更新帖子
export function updatePost(id, data) {
  return request({
    url: `/forum/post/${id}`,
    method: 'put',
    data
  })
}

// 删除帖子
export function deletePost(id) {
  return request({
    url: `/forum/post/${id}`,
    method: 'delete'
  })
}

// 删除帖子（使用新接口）
export function deleteForumPost(id) {
  return request({
    url: '/forum/post/delete',
    method: 'delete',
    params: {
      id: id
    }
  })
}

// 获取帖子评论
export function getPostComments(postId, params) {
  return request({
    url: `/forum/post/${postId}/comments`,
    method: 'get',
    params
  })
}

// 添加评论
export function addComment(postId, data) {
  return request({
    url: `/forum/post/${postId}/comment`,
    method: 'post',
    data
  })
}

// 删除评论
export function deleteComment(commentId) {
  return request({
    url: `/forum/comment/${commentId}`,
    method: 'delete'
  })
}

// 点赞帖子
export function likePost(id) {
  return request({
    url: `/forum/post/${id}/like`,
    method: 'post'
  })
}

// 点赞或取消点赞帖子
export function togglePostLike(postId, userId) {
  return request({
    url: '/forum/post/like',
    method: 'post',
    params: {
      postId,
      userId
    }
  })
}

// 点赞或取消点赞评论
export function toggleCommentLike(commentId, userId) {
  return request({
    url: '/forum/comment/like',
    method: 'post',
    params: {
      commentId,
      userId
    }
  })
}

// 获取论坛分类
export function getForumCategories() {
  return request({
    url: '/forum/categories',
    method: 'get'
  })
}

// 获取帖子分类（分页）
export function getForumCategoryPage(params) {
  return request({
    url: '/forum/forum_category/page',
    method: 'get',
    params,
    skipAuth: true
  })
}

// 获取排行榜分类
export function getRankingCategoryPage(params) {
  return request({
    url: '/rank/forum_category/page',
    method: 'get',
    params,
    skipAuth: true
  })
}

// 获取排行榜分类下的内容
export function getRankingPersonPage(params) {
  return request({
    url: '/rank/person/page',
    method: 'get',
    params,
    skipAuth: true
  })
}

// 获取排行榜人物详情
export function getRankingPersonDetail(id) {
  return request({
    url: '/rank/person/get',
    method: 'get',
    params: { id },
    skipAuth: true
  })
}

// 获取帖子分页
export function getPostPage(params) {
  return request({
    url: '/forum/post/page',
    method: 'get',
    params,
    skipAuth: true
  })
}

// 获取帖子详情
export function getPostDetailById(id) {
  return request({
    url: '/forum/post/get',
    method: 'get',
    params: { id },
    skipAuth: true
  })
}

// 获取帖子评论树
export function getCommentTree(postId) {
  return request({
    url: '/forum/comment/list',
    method: 'get',
    params: { postId },
    skipAuth: true
  })
}

// 获取排行榜评论树
export function getRankingCommentTree(personId) {
  return request({
    url: '/rank/comment/list',
    method: 'get',
    params: { personId },
    skipAuth: true
  })
}

// 发表评论或回复
export function addForumComment(data) {
  return request({
    url: '/forum/comment/add',
    method: 'post',
    data
  })
}

// 发表排行榜评论或回复
export function addRankingComment(data) {
  return request({
    url: '/rank/comment/add',
    method: 'post',
    data
  })
}

// 上传文件
export function uploadFile(file) {
  const formData = new FormData()
  formData.append('file', file)

  return request({
    url: '/system/file/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 创建排行榜人物
export function createRankingPerson(data) {
  return request({
    url: '/rank/person/create',
    method: 'post',
    data
  })
}

// 创建评分
export function createRankingScore(data) {
  return request({
    url: '/rank/score/create',
    method: 'post',
    data
  })
}

// 点赞或取消点赞排行榜评论
export function toggleRankingCommentLike(commentId, userId) {
  return request({
    url: '/rank/comment/like',
    method: 'post',
    params: {
      commentId,
      userId
    }
  })
}

// 获取我的评论列表
export function getMyComments(userId) {
  return request({
    url: '/forum/comment/my_comment',
    method: 'get',
    params: {
      userId
    }
  })
}

// 删除论坛评论
export function deleteForumComment(commentId, userId) {
  return request({
    url: '/forum/comment/delete',
    method: 'delete',
    params: {
      commentId: commentId,
      userId: userId
    }
  })
}